﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema
{
    public partial class QuanLyQuocGia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsQG();
        }

        public void LaydsQG()
        {
            DataTable ds_qg = Timkiem_MA("");

            for (int i = 0; i < ds_qg.Rows.Count; i++)
            {
                string str = ds_qg.Rows[i]["Maqg"].ToString();
                DDLMaQG.Items.Add(str);
            }
            grQuocGia.DataSource = ds_qg;
            grQuocGia.DataBind();
        }

        public DataTable Timkiem_Ten(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@tenQG", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_QUOCGIA_TENQG", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_QUOCGIA_TENQG");
        }
        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAQG", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_QUOCGIA_MAQG", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_QUOCGIA_MAQG");
        }




        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // string s = "select * from quocgia where maqg='" + DDLMaQG.SelectedItem.ToString() + "'";
            DataTable dt = Timkiem_MA(DDLMaQG.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtTenQG.Text = dt.Rows[0][1].ToString();
            }
        }

        protected void grQuocGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLMaQG.Text = grQuocGia.SelectedRow.Cells[1].Text;
            txtTenQG.Text = grQuocGia.SelectedRow.Cells[2].Text;
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@tenqg", txtTenQG.Text.ToString()));
                CDatastore.ThucHienStore("SP_INSERT_QUOCGIA", sqlPara);
                lblThongBao.Text = "Bạn đã thêm Quốc Gia thành công";
                DDLMaQG.Items.Clear();
                txtTenQG.Text = null;
                LaydsQG();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@maqg", DDLMaQG.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_QUOCGIA", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Quốc Gia " + DDLMaQG.Text + " thành công";
                DDLMaQG.Items.Clear();
                LaydsQG();
                txtTenQG.Text = null;

            }
            catch (Exception ex)
            {
                lblThongBao.Text = "Quốc Gia đã tồn tại trên Phim không thể xóa";
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@maqg", DDLMaQG.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tenqg", txtTenQG.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_QUOCGIA", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Quốc Gia " + DDLMaQG.Text + " thành công";
                DDLMaQG.Items.Clear();
                LaydsQG();
                txtTenQG.Text = null;
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void BTTIMKIEM_Click(object sender, EventArgs e)
        {
            DataTable dt = Timkiem_Ten(txtTenQG.Text);
            grQuocGia.DataSource = dt;
            grQuocGia.DataBind();
        }

        protected void grQuocGia_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grQuocGia.PageIndex = e.NewPageIndex;
            grQuocGia.DataBind();
            LaydsQG();
        }
    }
}